import type { AxiosInstance } from 'axios'
import { initService } from './Request'
// import service from './Request'
import qs from 'qs'

let service: AxiosInstance
init()
async function init() {
    service = await initService()
}

export const GET = (url: string, data?: any): any => {
    if (url === '/captcha') {
        return service.get(url, {
            responseType: 'arraybuffer',
            params: data
        })
    } else if (url.includes('download')) {
        return service({
            url,
            method: 'get',
            responseType: 'blob',
            params: data,
        })
    } else {
        return service.get(url, {
            params: data
        })
    }
}


// isParams 是否需要序列化
export const POST = (url: string, data?: any, isParams?: boolean, config?: Object) => {
    // console.log(url, data, isParams, config);

    if (url.includes('download')) {
        if (isParams) {
            return service({
                url,
                method: 'post',
                responseType: 'blob',
                data: qs.stringify(data),
            })
        } else {
            return service({
                url,
                method: 'post',
                responseType: 'blob',
                data
            })
        }
    } else if (isParams) {
        return service.post(url, qs.stringify(data), config ? config : {})
    } else {
        // console.log(url, data, config);
        return service.post(url, data, config ? config : {})
    }
}